#include <math.h>

class StatCalc
{
	double m_Total;
	double m_SqTotal;
	size_t m_n;

 public:
	StatCalc() : m_Total( 0.0 ), m_SqTotal( 0.0 ), m_n( 0 )
		{
		}

	StatCalc& operator+= ( double d )
		{
			m_Total += d;
			m_SqTotal += d*d;
			++m_n;
			return *this;
		}

	void clear()
		{
			m_Total = 0.0;
			m_SqTotal = 0.0;
			m_n = 0;
		}

	double mean() const // m_n must be non-zero
		{
			return m_Total / m_n;
		}

	double variance() const
		{
			return (m_SqTotal - m_Total * m_Total / m_n) / m_n;
		}

	double sd() const
		{
			return ::sqrt( variance() );
		}
};

inline StatCalc operator+( const StatCalc & lhs, double rhs )
{
	StatCalc res( lhs );
	return res += rhs;
}
